package com.lanlian.cayman.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.lanlian.cayman.R;
import com.lanlian.cayman.adapter.TradeDetailsEntrustAdapter;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.cayman.view.DetailsFilterWindow;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.EntrustDetailsInfo;
import com.lanlian.commonlib.bean.ReqEntrustDetails;
import com.lanlian.commonlib.bean.RespEntrustDetails;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.utillib.DateUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/14 16:51
 *     desc   : 委托明细
 *     version: 1.0
 * </pre>
 */
public class TradeDetailsEntrustActivity extends BaseActivity implements View.OnClickListener {

    private SmartRefreshLayout mLayRefresh;
    private RecyclerView mRecycleView;
    private ArrayList<EntrustDetailsInfo> mList = new ArrayList<>();
    private TradeDetailsEntrustAdapter mAdapter;

    private String mBeginDate;
    private String mEndDate;
    private DetailsFilterWindow mPopupWindow;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_trade_details_entrust, true);
        initView();
        setListeners();
        initAdapter();
        mLayRefresh.autoRefresh();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("委托明细");
        setRightImage(R.mipmap.detailed_icon_screen);
        mLayRefresh = findViewById(R.id.lay_refresh);
        mRecycleView = findViewById(R.id.recycle_view);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mImgAction.setOnClickListener(this);
        mLayRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mRecycleView.scrollToPosition(0);
                getEntrustDetails();
            }
        });
    }

    private void initAdapter() {
        mAdapter = new TradeDetailsEntrustAdapter(R.layout.adapter_trade_details_entrust, mList);
        mRecycleView.setAdapter(mAdapter);
        mRecycleView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter.bindToRecyclerView(mRecycleView);
        mAdapter.setEmptyView(R.layout.lay_empty_details);
    }

    private void getEntrustDetails() {
        ReqEntrustDetails reqEntrustDetails = new ReqEntrustDetails();
        reqEntrustDetails.setBeginDate(mBeginDate);
        reqEntrustDetails.setEndDate(mEndDate);
        reqEntrustDetails.setToken(mDBManager.getToken());
        VolleyRequestUtils.getInstance().getEntrustDetails(this.toString(), reqEntrustDetails,
                new INetWorkCallback() {
                    @Override
                    public void onSuccess(Object response) {
                        mLayRefresh.finishRefresh();
                        RespEntrustDetails respEntrustDetails = (RespEntrustDetails) response;
                        mAdapter.setNewData(respEntrustDetails.getDataList());
                    }

                    @Override
                    public void onFail(BaseResponse baseResponse) {
                        mLayRefresh.finishRefresh();
                        showToast(baseResponse.getMsg());
                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_action:
                if (mPopupWindow == null) {
                    mPopupWindow = new DetailsFilterWindow(this, new DetailsFilterWindow.OnFilterListener() {
                        @Override
                        public void onFilter(long beginDate, long endDate) {
                            mBeginDate = DateUtils.longToString(beginDate, "yyyy-MM-dd 00:00:00");
                            mEndDate = DateUtils.longToString(endDate, "yyyy-MM-dd 24:00:00");
                            mLayRefresh.autoRefresh();
                        }
                    });
                }
                mPopupWindow.showAsDropDown(v);
                break;
            default:
                break;
        }
    }

}
